--[[
@module	Sled.layers command
 layers [current id] [list new rename merge]
]]
local lume = require(LIB_PATH.."lume")
local Command = require "command"
return Command("layer", {
	
	{ "list", function ()
		local out = ""
		local sep = ", "
		local layers_tbl = Sled.current_map.layers.tbl
		for k, l in ipairs(layers_tbl) do
			if k == #layers_tbl then sep = "." end
			if l == Sled.current_layer then out = out .. "*" end
			out = out .. l.name .. " z: [" .. l.z .. "]" .. sep
		end
		return out
	end },
		
	{	"new", function (layer_name)
		if not Sled.current_map then return "no current map" end
		Sled.current_map.layers:add(layer_name)
	end
	},
	
	{ "merge", function(layer_name)
		if not layer_name then
			return "no name given"
		end
		local l = Sled.current_map.layers:get(layer_name)
		Sled.current_layer:paste(l)
		lume.remove(Sled.current_map.layers.tbl, l)
	end
	},
	
	{ "switch", function (id)
		if not id then return "no name given" end
		local layer = Sled.current_map.layers:get(id)
		if not layer then return "no layer " .. id .. "found"
		else Sled.current_layer = layer
		end
	end
	},
	
	{ "clear", function (l_name)
		local layer = l_name and Sled.current_map.layers:get(l_name) or Sled.current_layer
		layer:clear()
		return "Layer " .. layer.name .. " cleared."
	end
	},
	
	{ "discard", function (id)
		if not id then return "no name given" end
		local layer = Sled.current_map.layers:get(id)
		if not layer then return "no layer " .. id .. " found"
		else 
			Sled.current_map.layers:discard(id)
			return "layer " .. id.. " discarded"
		end
	end,
	},

    { "rename", function (name)
        if not name then return "no name given" end
        local layer = Sled.current_layer
        local n = layer.name
        layer.name = name
        return n .. " renamed to " .. layer.name
    end
    }
})
